package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.converter.CodeConfigConverter;
import com.youlai.system.mapper.SysCodeConfigMapper;
import com.youlai.system.model.entity.SysCodeConfig;
import com.youlai.system.model.query.CodeConfigQuery;
import com.youlai.system.service.SysCodeConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Guijiang.Lai
* @description 针对表【sys_code_config】的数据库操作Service实现
* @createDate 2025-07-15 13:35:55
*/
@Service
@RequiredArgsConstructor
public class SysCodeConfigServiceImpl extends ServiceImpl<SysCodeConfigMapper, SysCodeConfig>
    implements SysCodeConfigService{

    private final CodeConfigConverter codeConfigConverter;

    @Override
    public boolean deleteCodeConfig(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的用户数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public List<SysCodeConfig> getCodeConfigByIds(String idsStr) {
        return null;
    }

    @Override
    public Page<SysCodeConfig> getCodeConfigPage(CodeConfigQuery query) {
        int pageNum = query.getPageNum();
        int pageSize = query.getPageSize();
        String keywords = query.getKeywords();

        LambdaQueryWrapper<SysCodeConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.like(StrUtil.isNotBlank(keywords),SysCodeConfig::getName,keywords);

        Page<SysCodeConfig> sysCodeConfigPage = this.page(
                new Page<>(pageNum,pageSize),
                lambdaQueryWrapper

        );

        Page<SysCodeConfig> pageResult =  codeConfigConverter.entity2Page(sysCodeConfigPage);
        return pageResult;


    }
}




